import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:like_button/like_button.dart';
import 'package:provide/provide.dart';
import 'package:qgchat/common/class/toUserInfo/toUserInfo.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/common.dart';
import 'package:qgchat/http/models/dtListModel.dart';
import 'package:qgchat/http/models/resModel.dart';
import 'package:qgchat/states/states_model/user.dart';
import 'package:qgchat/widgets/due/dif.dart';
import 'package:qgchat/widgets/dynamic/comment.dart';
import 'package:qgchat/widgets/dynamic/squareAvatar.dart';
import 'package:qgchat/widgets/dynamic/squareImgs.dart';
import 'package:qgchat/widgets/easyRefreshWidget.dart';
import 'package:qgchat/common/class/date/index.dart';
import 'package:qgchat/widgets/tipsDialog.dart';

class SquareView extends StatefulWidget {
  SquareView({Key key}) : super(key: key);

  @override
  _SquareViewState createState() => _SquareViewState();
}

class _SquareViewState extends State<SquareView> {
  int page = 1;

  List<DtListModel> state = [];

  /// 获取数据
  Future getList() async {
    final list = await CommonApis.dtList(page: page);
    state.addAll(list);
    setState(() {});
  }

  /// 点赞
  Future<bool> like(bool isLiked, int id) async {
    ResModel res;
    if (isLiked) {
      res = await CommonApis.canceldtdz(id);
    } else {
      res = await CommonApis.dtdz(id);
    }
    return res.status == 1 && !isLiked;
  }

  @override
  void initState() {
    super.initState();
    getList();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: EasyRefresh(
        header: EasyRefreshSetting.header,
        footer: EasyRefreshSetting.footer,
        onRefresh: () async {
          page = 1;
          state = [];
          getList();
        },
        onLoad: () async {
          page++;
          getList();
        },
        child: ListView.builder(
          itemCount: state.length,
          itemBuilder: (BuildContext context, int i) {
            return Container(
              margin: EdgeInsets.only(bottom: px(15)),
              padding: EdgeInsets.all(px(30)),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border(bottom: BorderSide(color: col('fefefe'))),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  // 头像 昵称 时间
                  Container(
                    height: px(90),
                    child: Row(
                      children: <Widget>[
                        GestureDetector(
                          onTapUp: (val) {
                            ToUserInfo.show(context, state[i].memberToken);
                          },
                          child: SquareAvatar(avatarUrl: state[i].memberHead),
                        ),
                        Container(
                          margin: EdgeInsets.only(left: px(20)),
                          padding: EdgeInsets.symmetric(vertical: px(7)),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "${state[i].memberNickname}",
                                style: TextStyle(
                                  color: col("303133"),
                                  fontSize: px(30),
                                ),
                              ),
                              Text(
                                "${Date.simple(int.parse(state[i].createtime))}",
                                style: TextStyle(
                                  color: col("bfc2cc"),
                                  fontSize: px(24),
                                ),
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  // 配文
                  If(
                    dif: state[i].text != null,
                    ifView: Container(
                      margin: EdgeInsets.only(top: px(20)),
                      child: Text(
                        "${state[i].text}",
                        style: TextStyle(
                          color: col("303133"),
                          fontSize: px(30),
                        ),
                      ),
                    ),
                  ),
                  // 配图
                  if (state[i].img != null)
                    Container(
                      margin: EdgeInsets.only(top: px(20)),
                      child: SquareImgs(imgs: state[i].img),
                    ),
                  // 视频
                  if (state[i].video != null)
                    GestureDetector(
                      onTap: () {
                        goPage(
                          context,
                          "/videoPlay",
                          param: {"url": state[i].video},
                        );
                      },
                      child: Container(
                        constraints: BoxConstraints(
                          maxWidth: px(300),
                          maxHeight: px(400),
                        ),
                        margin: EdgeInsets.only(top: px(20)),
                        child: Stack(
                          alignment: Alignment.center,
                          children: [
                            Image.network(
                              '${state[i].video}?vframe/jpg/offset/0/h/400',
                            ),
                            Container(
                              width: px(60),
                              height: px(60),
                              decoration: BoxDecoration(
                                color: col('24d4f2'),
                                borderRadius: BorderRadius.circular(px(30)),
                              ),
                              child: Icon(
                                Icons.play_arrow,
                                color: Colors.white,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  // 点赞数 评论数
                  Container(
                    margin: EdgeInsets.only(top: px(20)),
                    child: Row(
                      children: <Widget>[
                        LikeButton(
                          isLiked: state[i].type == 1,
                          onTap: (bool isLiked) async {
                            final user = Provide.value<UserState>(context);
                            final info = user.info;
                            final isLogin = info != null;
                            if (isLogin) {
                              return await like(isLiked, state[i].id);
                            } else {
                              TipsDialog.login(
                                context,
                                () {
                                  goPage(context, '/phoneLogin');
                                },
                              );
                            }
                          },
                          likeBuilder: (bool isLiked) {
                            String likesUrl = "assets/images/icon/likes.png";
                            String nolikesUrl =
                                "assets/images/icon/nolikes.png";
                            return Image.asset(
                              "${isLiked ? likesUrl : nolikesUrl}",
                              width: px(32),
                              height: px(30),
                            );
                          },
                          likeCount: state[i].memberFollow,
                          size: px(32),
                        ),
                        SquareComment(
                          state[i].comcount,
                          onPressed: () {
                            final user = Provide.value<UserState>(context);
                            final info = user.info;
                            final isLogin = info != null;
                            if (isLogin) {
                              goPage(context, '/dynamic', param: {
                                "id": state[i].id,
                              });
                            } else {
                              TipsDialog.login(
                                context,
                                () {
                                  goPage(context, '/phoneLogin');
                                },
                              );
                            }
                          },
                        ),
                      ],
                    ),
                  ),
                  // 评论
                  Container(
                    margin: EdgeInsets.only(top: px(20)),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: state[i].comment.map<Widget>(
                        (item) {
                          return Container(
                            height: px(36),
                            child: Text(
                              "${item.memberNickname}：${item.text}",
                              style: TextStyle(
                                color: col("303133"),
                                fontSize: px(26),
                              ),
                            ),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                  // 查看评论
                  Container(
                    margin: EdgeInsets.only(top: px(20)),
                    child: GestureDetector(
                      onTapUp: (val) {
                        final user = Provide.value<UserState>(context);
                        final info = user.info;
                        final isLogin = info != null;
                        if (isLogin) {
                          goPage(context, '/dynamic', param: {
                            "id": state[i].id,
                          });
                        } else {
                          TipsDialog.login(
                            context,
                            () {
                              goPage(context, '/phoneLogin');
                            },
                          );
                        }
                      },
                      child: Text(
                        "查看${state[i].comcount}条评论",
                        style: TextStyle(
                          color: col("909399"),
                          fontSize: px(24),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            );
          },
        ),
      ),
    );
  }
}
